import axios from 'axios';
import qs from 'query-string';
import type { HttpResponse } from './interceptor';

export interface CompanyId {
    id: number;
}

export interface CompanyRecord {
    id: number;
    name: string;
    city_id: number | null;
    city_name: string | null;
    person_total: number;
    master_name: string;
    master_phone: string;
    address: string;
    remark: string | null;
    created_at: string;
    updated_at: string;
}

export interface CompanyListRes {
    list: CompanyRecord[];
    total: number;
}

export interface CompanyModel {
    id?: number;
    name: string;
    city_id: number | null;
    person_total?: number;
    master_name?: string;
    master_phone?: string;
    address?: string;
    remark?: string;
}

export interface CompanyParams extends Partial<CompanyRecord> {
    page: number;
    pageSize: number;
}

export interface CityOption {
    id: number;
    name: string;
}

/**
 * 获取公司列表
 * @param params
 */
export function queryCompanyList(params: CompanyParams): Promise<HttpResponse<CompanyListRes>> {
    return axios.get('/admin/company/index', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 获取公司详情
 * @param params
 */
export function queryCompanyOne(params: CompanyId): Promise<HttpResponse<CompanyRecord>> {
    return axios.get('/admin/company/detail', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 获取城市列表（用于下拉选择）
 */
export function queryCityList(): Promise<HttpResponse<CityOption[]>> {
    return axios.get('/admin/company/cityList');
}

/**
 * 提交公司表单（创建/更新）
 * @param data
 */
export function submitCompanyForm(data: CompanyModel): Promise<HttpResponse<CompanyRecord>> {
    return axios.post('/admin/company/submit', data);
}

/**
 * 删除公司
 * @param id
 */
export function delCompany(id: number): Promise<HttpResponse<null>> {
    return axios.post('/admin/company/delete', { id });
} 

/**
 * 获取所有公司列表（不分页）
 */
export function queryAllCompanyList(): Promise<HttpResponse<CompanyRecord[]>> {
    return axios.get('/admin/company/all');
}

// 单位客服相关接口
export interface CompanyKfRecord {
    id: number;
    realname: string;
    phone: string;
    company_ids: string;
    status: number;
    created_at: string;
    updated_at: string;
    company_list: Array<{
        id: number;
        name: string;
    }>;
}

export interface CompanyKfListRes {
    list: CompanyKfRecord[];
    total: number;
}

export interface CompanyKfModel {
    id?: number;
    realname: string;
    phone: string;
    company_ids?: number[];
    status?: number;
}

export interface CompanyKfParams extends Partial<CompanyKfRecord> {
    page: number;
    pageSize: number;
}



/**
 * 获取单位客服列表
 * @param params
 */
export function queryCompanyKfList(params: CompanyKfParams): Promise<HttpResponse<CompanyKfListRes>> {
    return axios.get('/admin/company/kfList', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 获取单位客服详情
 * @param params
 */
export function queryCompanyKfOne(params: { id: number }): Promise<HttpResponse<CompanyKfRecord>> {
    return axios.get('/admin/company/kfDetail', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 提交单位客服表单（创建/更新）
 * @param data
 */
export function submitCompanyKfForm(data: CompanyKfModel): Promise<HttpResponse<CompanyKfRecord>> {
    return axios.post('/admin/company/kfSubmit', data);
}

/**
 * 删除单位客服
 * @param id
 */
export function delCompanyKf(id: number): Promise<HttpResponse<null>> {
    return axios.post('/admin/company/kfDelete', { id });
}


